package dq.study.minimvc.handler;

import dq.study.minimvc.HandlerExecutionChain;
import dq.study.minimvc.HandlerInterceptor;
import dq.study.minimvc.HandlerMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

public abstract class AbstractHandlerMapping implements HandlerMapping {

    private List<HandlerInterceptor> adaptedInterceptors = new ArrayList<>();

    public AbstractHandlerMapping() {
        initHandlerInterceptors();
    }

    private void initHandlerInterceptors(){
        //TODO 添加拦截器
    }

    @Override
    public HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
        Object handler = getHandlerInternal(request);
        return getHandlerExecutionChain(handler, request);
    }

    protected HandlerExecutionChain getHandlerExecutionChain(Object handler, HttpServletRequest request){
        HandlerExecutionChain chain =  new HandlerExecutionChain(handler);
        chain.addInterceptors(adaptedInterceptors);
        return chain;
    }


    protected abstract Object getHandlerInternal(HttpServletRequest request) throws Exception;
}
